package com.dmgis.qxfw.dataservice.businessservice.picturequiry.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dmgis.qxfw.dataservice.businessservice.api.entity.xt.sys_config;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.base_product;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.picturequiry.service.PictureQuiryService;
import com.dmgis.qxfw.dataservice.commonservice.common.DbConfig;
import com.dmgis.qxfw.dataservice.commonservice.dmgis_pg_srv.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.commonservice.utils.JsonUtil;
import com.dmgis.qxfw.dataservice.commonservice.utils.StringUtil;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;

@Service("PictureService")
public class PictureQuiryServiceImpl implements PictureQuiryService {
	ProcessRequestController pc = new ProcessRequestController();
	@Override
	public List<sys_config> getSysConfig(String ParamType) {
		// TODO 获取系统参数配置信息
//		DbContextHolder.setDbType("xt");
//		return configMapper.getsys_config(ParamType);
		List<sys_config> resultList = new ArrayList<sys_config>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("paramtype");
		columns.add("paramname");
		columns.add("paramvalue");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (ParamType!=null) {
			strWhere.append(String.format(" and paramtype='%s'",ParamType));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "sys_config");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			resultList = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<sys_config>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return resultList;
	}

	@Override
	public List<base_product> getLightPic() {
		// TODO 获得雷电
		List<base_product> resultList = new ArrayList<base_product>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("id");
		columns.add("forecasttime");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 条件where
		StringBuilder strWhere = new StringBuilder();
		strWhere.append(String.format(" producttype = '潜势预报'"));
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[] { "forecasttime desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "base_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode",DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			resultList = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<base_product>>() {
			});
		} catch (Exception e) {
			System.out.println("error!");
		}
		return resultList;
	}
}
